package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public abstract class BaseTeacherController {
    static TeacherService teacherService = new TeacherService();
    Scanner scanner = new Scanner(System.in);
    public void start() {

        teacher:while (true){
            System.out.println("--------欢迎来到 <教师> 管理系统--------");
            System.out.print("请输入您的选择: 1.添加教师  2.删除教师  3.修改教师  4.查看教师  5.退出     ");
            String choice = scanner.next();
            switch (choice){
                case "1":
//                    System.out.println("添加教师");
                    addTeacher();
                    break;
                case "2":
//                    System.out.println("删除教师");
                    deleteTeacher();
                    break;
                case "3":
//                    System.out.println("修改教师");
                    updateTeacher();
                    break;
                case "4":
//                    System.out.println("查询教师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出成功");
                    System.out.println("欢迎下次使用");
                    break teacher;
                default:
                    System.out.println("输入有误");
                    System.out.println("请重新输入");
                    break;
            }
        }

    }

    private void updateTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null){
            System.out.println("暂无数据");
            System.out.println("无法执行修改操作");
            System.out.println("欢迎添加");
        }else{
            System.out.println("请输入待更新数据ID");
            String tid = scanner.next();
            boolean exist = teacherService.isExist(tid);
            if (exist){
                Teacher teacher = getTeacher(tid);
                teacherService.updateTeacher(teacher);
                System.out.println("更新成功");
            }else{
                System.out.println("输入ID不存在");
                System.out.println("请重新输入");
            }
        }

    }

    private void deleteTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null){
            System.out.println("暂无数据");
            System.out.println("无法执行删除操作");
            System.out.println("欢迎添加");
        }else{
            System.out.println("请输入需删数据的ID");
            while(true){
                String tid = scanner.next();
                boolean exist = teacherService.isExist(tid);
                if (exist){
                    teacherService.deleteTeacher(tid);
                    System.out.println("删除成功");
                    break;
                }else {
                    System.out.println("输入ID不存在");
                    System.out.println("请重新输入");
                }
            }
        }
    }

    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null){
            System.out.println("暂无信息");
            System.out.println("欢迎添加");
        }else {
            for (int i = 0; i < teachers.length; i++) {
                if (teachers[i] != null){
                    System.out.println(teachers[i].toString());
                }
            }
        }
    }

    private void addTeacher() {
        boolean full = isFull();
        if (full){
            System.out.println("空间已满");
            System.out.println("添加失败");
        }else {
            String tid;
            System.out.print("请输入教师id信息");

            while (true){
                tid = scanner.next();
                boolean exist = teacherService.isExist(tid);
                if (exist){
                    System.out.println("Id已存在");
                    System.out.print("请重新输入");
                }else {
                    break;
                }
            }

            Teacher teacher = getTeacher(tid);
            boolean success = teacherService.addTeacher(teacher);

            if (success == true){
                System.out.println("添加教师成功");
            }else {
                System.out.println("添加教师失败");
            }
        }
    }

    private boolean isFull() {
        boolean full = teacherService.isFull();
        return full;
    }

    public abstract Teacher getTeacher(String tid);
}
